<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/31
 * Time: 11:47
 */

namespace App\Http\Controllers\Api\V1;

use App\Models\CarLocation;
use App\Models\Coach;
use App\Models\Comment;
use App\Models\Complaint;
use App\Models\Period;
use App\Models\PutPeriod;
use App\Models\School;
use App\Models\Student;
use App\Models\StudentCount;
use App\Models\StudentScore;
use App\Models\Subscribe;
use App\Models\Teacharea;
use App\Models\Trainingcar;
use App\Models\User;
use App\Models\Message;
use App\Models\StudentCoach;
use App\Models\StudentState;
use App\Models\TcpStudytime;
use App\Repositories\CoachsRepository;
use App\Repositories\EvaluationRepository;
use App\Repositories\SchoolRepository;
use App\Repositories\StudentRepository;
use App\Repositories\MessageRepository;
use App\Repositories\StudytimeRepository;
use App\Repositories\SubscribesRepository;
use App\Repositories\StudentCountRepository;
use App\Repositories\CommentsRepository;
use App\Repositories\StudentStateRepository;
use App\Repositories\OrderListRepository;
use App\Repositories\PutimesRepository;
use App\Repositories\TemplateRepository;
use App\Transformers\CoachDetailsTransformer;
use App\Transformers\EvaluationTransformer;
use App\Transformers\FeedbackTransformer;
use App\Transformers\MessageTransformer;
use App\Transformers\OperationTransformer;
use App\Transformers\PeriodCoachTransformer;
use App\Transformers\PeriodTransformer;
use App\Transformers\SubscribeTransformer;
use App\Transformers\MySubscribeTransformer;
use App\Transformers\PaymentDetailTransformer;
use App\Models\SchoolConfig;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Libs\Umeng;
use App\Transformers\UserTransformer;
use App\Repositories\ServiceRepository;

class SchoolController extends BaseController
{
    protected $study;
    protected $coachs;
    protected $evaluation;
    protected $subscribe;
    protected $student;
    protected $message;
    protected $comments;
    protected $student_count;
    protected $student_state;
    protected $order_list;
    protected $period;
    protected $service;
    protected $putimes;
    protected $template;
    protected $school;

    public function __construct(
        StudytimeRepository $studytimeRepository,
        CoachsRepository $coachsRepository,
        EvaluationRepository $evaluationRepository,
        SubscribesRepository $subscribesRepository,
        StudentRepository $studentRepository,
        MessageRepository $messageRepository,
        CommentsRepository $comments,
        StudentCountRepository $studentCount,
        StudentStateRepository $studentState,
        OrderListRepository $order_list,
        StudytimeRepository $period,
        ServiceRepository $service,
        PutimesRepository $putimesRepository,
        TemplateRepository $template,
        SchoolRepository $school
    )
    {
        $this->student = $studentRepository;
        $this->study = $studytimeRepository;
        $this->coachs = $coachsRepository;
        $this->evaluation = $evaluationRepository;
        $this->subscribe = $subscribesRepository;
        $this->message = $messageRepository;
        $this->comments = $comments;
        $this->student_count = $studentCount;
        $this->student_state = $studentState;
        $this->order_list = $order_list;
        $this->period = $period;
        $this->service = $service;
        $this->putimes = $putimesRepository;
        $this->template = $template;
        $this->school = $school;
    }


    /**
     * @api {get} /school/index  驾校首页
     * @apiDescription  驾校首页
     * @apiGroup School
     * @apiSampleRequest /api/school/index
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function index(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

//        if (!$this->checkCoachToken($user->item, JWTAuth::getToken())) {
//            return $this->errorResponse('你的账号在其他地方登录', 401);
//        }
        $data = $this->school->index($user->item);

        return $this->successResponse('success', ['data' => $data]);
    }



    /**
     * @api {get} /school/subscribeList  教练预约管理列表
     * @apiDescription  教练预约管理列表
     * @apiGroup School
     * @apiSampleRequest /api/school/subscribeList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {String} [date]  日期
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function subscribeList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

//        if (!$this->checkCoachToken($user->item, JWTAuth::getToken())) {
//            return $this->errorResponse('你的账号在其他地方登录', 401);
//        }
        $list = $this->school->subscribeList($user->item, $request->input('date'));

        return $this->successResponse('success', ['data' => $list]);
    }


    /**
     * @api {get} /school/coachPeriod  教练预约详情列表
     * @apiDescription  教练预约详情列表
     * @apiGroup School
     * @apiSampleRequest /api/school/coachPeriod
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {String} [date]  日期
     * @apiParam {Int} [coach_id]  教练id
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function coachPeriod(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $list = $this->coachs->coachPeriod($request->input('coach_id'), $request->input('date'));

        return $this->successResponse('success', ['data' => $list]);
    }


    /**
     * @api {get} /school/coachStudent  教练绑定学员
     * @apiDescription  教练绑定学员
     * @apiGroup School
     * @apiSampleRequest /api/school/coachStudent
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [coach_id]  教练id
     * @apiParam {Int} [km]  科目
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function coachStudent(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $coach_info = Coach::find($request->input('coach_id'));

        $rs = $this->subscribe->studentSublist($coach_info, $request->input('km'));

        if ($rs['state']) {
            return $this->successResponse('success', ['data' => $rs['data']]);
        } else {
            return $this->errorResponse($rs['msg']);
        }
    }


    /**
     * @api {get} /school/studentSub  帮教练预约学员处理
     * @apiDescription  帮教练预约学员处理
     * @apiGroup School
     * @apiSampleRequest /api/school/studentSub
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [student_id]  学员id
     * @apiParam {Int} [ispick_up]  是否接送
     * @apiParam {Int} [pid]  学时id
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function studentSub(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $param = [
            'putime_id'=>$request->get('pid'),
            'student_id'=>$request->get('student_id'),
            'user_id'=>$user->id,
            'ispick_up'=>$request->get('ispick_up',0),
        ];

        $status_info = $this->subscribe->store($param, 'school');

        if($status_info !== false){

            if($status_info->km == 2){
                $km = '科二实操练习';
            }else{
                $km = '科三实操练习';
            }
            $msg['content'] = '驾校帮您预约了'.$status_info->coach->name.'教练：'.date('Y-m-d H:i', $status_info->startime).' ~ '.date('H:i', $status_info->endtime).' 的'.$km.'，要按时到达哦';
            $msg['from_type'] = 1;//消息来源学员
            $msg['from_id'] = $user->item->id;
            $msg['to_type'] = 1;//消息接受学员
            $msg['to_id'] = $param['student_id'];

            $student_info = Student::find($param['student_id']);
            if($student_info->device_type == 'ios'){
                $msg['device_type'] = 1;
            }elseif($student_info->device_type == 'android'){
                $msg['device_type'] = 2;
            }
            $this->message->store($msg);

            if( $student_info->device_token ){
                $umen = new Umeng($student_info->device_type, 3);
                $umen->sendNotificationToDevices(['title' => '预约提示', 'text' => $msg['content'], 'device_tokens' => $student_info->device_token]);
            }
            //发送给教练
            $msg['content'] = $student_info->name.'学员预约了您：'.date('Y-m-d H:i', $status_info->startime).' ~ '.date('H:i', $status_info->endtime).' 的'.$km;
            $msg['from_type'] = 1;//消息来源
            $msg['from_id'] = $user->item->id;
            $msg['to_type'] = 2;//消息接受教练
            $msg['to_id'] = $status_info->coach->id;
            $msg['device_type'] = 3;
            $this->message->store($msg);

            $coach_id = User::where('item_id',$status_info->coach->id)->where('item_type','App\Models\Coach')->value('id');
            $data['km'] = $status_info->km;
            $data['status'] = 1;
            $data['userId'] = $user->id;
            $data['name']  = $student_info->name;
            $data['phone'] = $student_info->phone;
            $data['cocahUserId'] = $coach_id;
            $data['cocahName'] = $status_info->coach->name;
            $data['cocahPhone'] = $status_info->coach->mobile;
            $data['location'] = '';
            $data['times'] = date('Y-m-d H:i', $status_info->startime).' ~ '.date('H:i', $status_info->endtime);
            $data['userType'] = 3;
            $this->subscribe->post_weixin_student_yy($data);
            return $this->successResponse('预约成功');
        }else{
            return $this->errorResponse($this->subscribe->error?$this->subscribe->error:'预约失败');
        }
    }



    /**
     * @api {post} /school/studentSubDel  帮学员取消预约
     * @apiDescription  帮学员取消预约
     * @apiGroup School
     * @apiSampleRequest /api/school/studentSubDel
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [putime_id]  学时id
     * @apiParam {String} [student_id]  学员id
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function studentSubDel(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token
        $putime_id = $request->get('putime_id', 0);
        $student_id = $request->get('student_id', 0);
        if (!$putime_id || !$student_id) {
            return $this->errorResponse('参数错误');
        }
        $status_info = $this->subscribe->deleAllPutime($putime_id, $student_id, 2);
        $student_info = $this->student->getById($student_id);


        if ($status_info !== false) {

            if ($status_info->km == 2) {
                $km = '科二实操练习';
            } else {
                $km = '科三实操练习';
            }
            $msg['content'] = '驾校为您取消了' . $status_info->coach->name . '教练：' . date('Y-m-d H:i', $status_info->startime) . ' ~ ' . date('H:i', $status_info->endtime) . ' 的' . $km;
            $msg['from_type'] = 1;//消息来源学员
            $msg['from_id'] = $student_info->school_id;
            $msg['to_type'] = 1;//消息接受学员
            $msg['to_id'] = $student_info->id;

            if ($student_info->device_type == 'ios') {
                $msg['device_type'] = 1;
            } elseif ($student_info->device_type == 'android') {
                $msg['device_type'] = 2;
            }

            $msg['created_at'] = date('Y-m-d H:i:s');
            Message::insert($msg);


            if ($student_info->device_token) {
                $umen = new Umeng($student_info->device_type, 3);
                $umen->sendNotificationToDevices(['title' => '预约取消提示', 'text' => $msg['content'], 'device_tokens' => $student_info->device_token]);
            }
            $msg['content'] = '驾校为您取消了' . $student_info->name . '学员：' . date('Y-m-d H:i', $status_info->startime) . ' ~ ' . date('H:i', $status_info->endtime) . ' 的' . $km;
            $msg['from_type'] = 1;//消息来源
            $msg['from_id'] = $student_info->school_id;
            $msg['to_type'] = 2;//消息接受教练
            $msg['to_id'] = $status_info->coach->id;
            $msg['device_type'] = 3;
            Message::insert($msg);
            $coach_id = User::where('item_id', $status_info->coach->id)->where('item_type', 'App\Models\Coach')->value('id');
            $user_id = User::where('item_id', $student_info->id)->where('item_type', 'App\Models\Student')->value('id');
            $student_count = StudentCount::where('student_id',$student_info->id)->first();
            if (!empty($coach_id) || !empty($user_id)) {
                $data['km'] = $status_info->km;
                $data['status'] = 2;
                $data['userId'] = $user_id;
                $data['name'] = $student_info->name;
                $data['phone'] = $student_info->phone;
                $data['cocahUserId'] = $coach_id;
                $data['cocahName'] = $status_info->coach->name;
                $data['cocahPhone'] = $status_info->coach->mobile;
                $data['location'] = '';
                $data['return_hours'] = $status_info->study_time;
                $data['surplus_hours'] = $student_count->km2period + $student_count->km3period;
                $data['times'] = date('Y-m-d H:i', $status_info->startime) . ' ~ ' . date('H:i', $status_info->endtime);
                $data['userType'] = 3;
                $this->subscribe->post_weixin_student_yy($data);
            }
            return $this->successResponse('取消成功');
        } else {
            return $this->errorResponse($this->subscribe->error ? $this->subscribe->error : '操作失败');
        }
    }



    /**
     * @api {get} /school/summaryReport  驾校综合报表
     * @apiDescription  驾校综合报表
     * @apiGroup School
     * @apiSampleRequest /api/school/summaryReport
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function summaryReport(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $data['total_month'] = $this->school->report($user->item, 0, time());
        $data['this_month'] = $this->school->report($user->item, strtotime(date('Y-m-01')), strtotime(date('Y-m-t 23:59:59')));
        $data['last_month'] = $this->school->report($user->item, strtotime(date('Y-m-01').' -1 month'), strtotime(date('Y-m-01 23:59:59').' -1 day'));

        return $this->successResponse('success', ['data' => $data]);
    }


    /**
     * @api {get} /school/searchReport  驾校搜索报表
     * @apiDescription  驾校搜索报表
     * @apiGroup School
     * @apiSampleRequest /api/school/searchReport
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {String} [start_time]  查询开始时间
     * @apiParam {String} [end_time]  查询结束时间
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function searchReport(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $data = $this->school->report($user->item, strtotime($request->input('start_time')), strtotime($request->input('end_time')));

        return $this->successResponse('success', ['data' => $data]);
    }


    /**
     * @api {get} /school/coachList  驾校教练列表
     * @apiDescription  驾校教练列表
     * @apiGroup School
     * @apiSampleRequest /api/school/coachList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [page] 第几页 默认第一页
     * @apiParam {Int} [size] 每页多少条 默认15
     * @apiParam {Int} [type] 排序 1默认预约工时数，2学员数，3好评率，4投诉数, 5工时数, 6待审核
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function coachList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $page = $request->input('page', 1);
        $size = $request->input('size', 15);
        $type = $request->input('type', 1);

        $coach_list = $this->coachs->coachList($user->item, $type, $page, $size);

        return $this->successResponse('success', ['data' => $coach_list]);
    }


    /**
     * @api {get} /school/carList  驾校车辆列表
     * @apiDescription  驾校车辆列表
     * @apiGroup School
     * @apiSampleRequest /api/school/carList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function carList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $car_list = Trainingcar::where('school_id', $user->item_id)->get();

        if($car_list){

            foreach($car_list as $car){

                $car_location = CarLocation::where('carnum', $car->carnum)->first();
                if ($car_location && (time() - strtotime($car_location->updated_at)) <  1800){
                    $car->is_online = 1;
                    $car->lat = $car_location->lat;
                    $car->lon = $car_location->lon;
                }else{
                    $car->is_online = 0;
                    $car->lat = 0;
                    $car->lon = 0;
                }
            }
        }

        return $this->successResponse('success', ['data' => $car_list]);
    }


    /**
     * @api {get} /school/carList  驾校车辆列表
     * @apiDescription  驾校车辆列表
     * @apiGroup School
     * @apiSampleRequest /api/school/carList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [page] 第几页 默认第一页
     * @apiParam {Int} [size] 每页多少条 默认15
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function studentList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $page = $request->input('page', 1);
        $size = $request->input('size', 15);

        $car_list = Trainingcar::where('school_id', $user->item_id)->limit($size)->offset(($page - 1) * $size)->get();

        return $this->successResponse('success', ['data' => $car_list]);
    }


    /**
     * @api {get} /school/studentInfo  驾校学员详情
     * @apiDescription  驾校学员详情
     * @apiGroup School
     * @apiSampleRequest /api/school/studentInfo
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [page] 第几页 默认第一页
     * @apiParam {Int} [size] 每页多少条 默认15
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public  function studentInfo(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $page = $request->input('page', 1);
        $size = $request->input('size', 15);

        $car_list = Trainingcar::where('school_id', $user->item_id)->limit($size)->offset(($page - 1) * $size)->get();

        return $this->successResponse('success', ['data' => $car_list]);
    }


    /**
     * @api {get} /school/commentList  驾校管理评论列表
     * @apiDescription  驾校管理评论列表
     * @apiGroup School
     * @apiSampleRequest /api/school/commentList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [page] 第几页 默认第一页
     * @apiParam {Int} [size] 每页多少条 默认15
     * @apiParam {String} [name] 搜索的姓名
     * @apiParam {String} [start_time] 开始时间
     * @apiParam {String} [end_time] 结束时间
     * @apiParam {Int} [type] 默认1全部，2差评
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function commentList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $page = $request->input('page', 1);
        $size = $request->input('size', 15);

        $param['name'] = $request->input('name');
        $param['start_time'] = $request->input('start_time');
        $param['end_time'] = $request->input('end_time');
        $param['type'] = $request->input('type', 1);

        $comment_list = $this->comments->getSchoolComment($user->item, $param, $page, $size);

        return $this->successResponse('success', ['data' => $comment_list]);
    }


    /**
     * @api {get} /school/complainList  驾校管理投诉列表
     * @apiDescription  驾校管理投诉列表
     * @apiGroup School
     * @apiSampleRequest /api/school/complainList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} [page] 第几页 默认第一页
     * @apiParam {Int} [size] 每页多少条 默认15
     * @apiParam {String} [name] 搜索的姓名
     * @apiParam {String} [start_time] 开始时间
     * @apiParam {String} [end_time] 结束时间
     * @apiParam {Int} [type] 默认1未处理，2已处理
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function complainList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $page = $request->input('page', 1);
        $size = $request->input('size', 15);

        $param['name'] = $request->input('name');
        $param['start_time'] = $request->input('start_time');
        $param['end_time'] = $request->input('end_time');
        $param['type'] = $request->input('type', 1);

        $comment_list = $this->student_state->getSchoolComplain($user->item, $param, $page, $size);

        return $this->successResponse('success', ['data' => $comment_list]);
    }


    /**
     * @api {get} /school/commentListReport  驾校管理评论报表
     * @apiDescription  驾校管理评论报表
     * @apiGroup School
     * @apiSampleRequest /api/school/commentListReport
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function commentListReport(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $comment_list = $this->comments->commentListReport($user->item);

        return $this->successResponse('success', ['data' => $comment_list]);
    }


    /**
     * @api {get} /school/teachareaList  驾校教学区域列表
     * @apiDescription  驾校教学区域列表
     * @apiGroup School
     * @apiSampleRequest /api/school/teachareaList
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function teachareaList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token


        $area_list = Teacharea::where('school_id', $user->item_id)->orderBy('type')->get();

        return $this->successResponse('success', ['data' => $area_list]);
    }


    /**
     * @api {get} /school/studentReport  学员报表
     * @apiDescription  学员报表
     * @apiGroup School
     * @apiSampleRequest /api/school/studentReport
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function studentReport()
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $list = $this->student->schoolStudentReport($user->item);

        return $this->successResponse('success', ['data' => $list]);
    }


    /**
     * @api {get} /school/coachCommentReport  教练评价报表
     * @apiDescription  教练评价报表
     * @apiGroup School
     * @apiSampleRequest /api/school/coachCommentReport
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function coachCommentReport()
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $list = $this->comments->coachCommentReport($user->item);

        return $this->successResponse('success', ['data' => $list]);
    }


    /**
     * @api {get} /school/coachDetail  教练详情查看
     * @apiDescription  教练详情查看
     * @apiGroup School
     * @apiSampleRequest /api/school/coachDetail
     * @apiPermission none
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Int} coach_id 教练id
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "message": "取消成功!",
     *       "status_code": 200
     *   }
     */
    public function coachDetail(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        $coach_info = Coach::find($request->input('coach_id'));

        if($coach_info){

            $coach_info->stu_count = StudentCoach::where('cid', $coach_info->id)->count(DB::raw('distinct(stuid)'));
            $coach_info->period_count = PutPeriod::where('coach_id', $coach_info->id)->sum('sum_period');
            $coach_info->comment_count = Comment::where('coach_id', $coach_info->id)->count();
            $good_count = Comment::where('coach_id', $coach_info->id)->where('comment','>',0)->count();
            $coach_info->comment_percent = $coach_info->comment_count ? number_format($good_count / $coach_info->comment_count, 2) : 0;
            $coach_info->complain_count = StudentState::where('coach_id', $coach_info->id)->count();
        }

        return $this->successResponse('success', ['data' => $coach_info]);

    }

}